﻿Imports System.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.Globalization
Imports Aricie.DNN.UI.Attributes
Imports Aricie.DNN.ComponentModel
Imports Aricie.DNN.UI.WebControls.EditControls
Imports DotNetNuke.Services.Localization

Namespace Aricie.DNN.Services.Url.FriendlyUrl
    <Serializable()> _
    Public Class UrlPartGroup
        Inherits VariableUrlGroup

        <AutoPostBack()> _
        <ExtendedCategory("MainSettings")> _
        <SortOrder(6)> _
        Public Property UrlPartName() As UrlPart = UrlPart.Scheme

        <ExtendedCategory("MainSettings")> _
       <SortOrder(7)> _
        Public ReadOnly Property UrlPartDescription As String
            Get
                Return Localization.GetString("UrlPart." & Me.UrlPartName.ToString(), FriendlierUrlContext.SharedResourceFile)
            End Get
        End Property

        Protected Overrides Function GetRawVariable(ByVal writer As UrlRewriteContext) As GroupRewriteResult
            Dim toReturn As New GroupRewriteResult
            Dim queryValue As String = Nothing
            Dim queryParam As UrlParam = UrlParam.FromSyntax(Me._UrlPartName.ToString())
            If writer.RewriteInput.Params.TryGetValue(queryParam, queryValue) Then
                toReturn.ConsumedParameters(queryParam) = True
                If Me.RewriteType = RewriteType.None Then
                    toReturn.RewriteValue = ""
                Else
                    toReturn.RewriteValue = queryValue
                    if queryValue.IsNullOrEmpty() AndAlso _UrlPartName = UrlPart.Fragment
                        toReturn.HasValidEmptyContent = True
                    End If
                End If
                writer.ParamsUsage(queryParam) = ParamState.Used
            End If
            Return toReturn
        End Function

        Protected Overrides Function GetName() As String
            Return Me.UrlPartName.ToString()
        End Function

    End Class

    Public Enum UrlPart
        Scheme
        RootPath
        Authority
        Host
        Port
        AbsolutePath
        LocalPath
        UserInfo
        PageName
        Extension
        QueryString
        Fragment
    End Enum
End Namespace


